package com.tjmc.tiomsresource.service.impl;

import com.tjmc.tiomsresource.service.OmcResDataService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-27 9:46
 */
@Service
public class OmcResDataServiceImpl implements OmcResDataService {

    private static Logger logger = LoggerFactory.getLogger(OmcResDataServiceImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<String> readSqlFile(String filePath) {
        List<String> sqls = new ArrayList<>();
        BufferedReader reader = null;
        InputStream inputStream = null;
        try {
            if(StringUtils.isBlank(filePath)) {
                logger.info("读取sql 文件, filePath错误: " + filePath);
                return null;
            }
            if(filePath.startsWith("classpath:")) {
                Resource resource = new ClassPathResource(filePath.substring("classpath:".length()));
                inputStream = resource.getInputStream();
//                File file = resource.getFile();
//                if(!file.exists()) {
//                    logger.info("读取sql 文件, 不存在: " + file.getAbsolutePath());
//                    return null;
//                }
//                filePath = file.getAbsolutePath();
            }
            logger.info("读取sql 文件: " + filePath);
            if(null != inputStream) {
                reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            } else {
                reader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(filePath), "UTF-8"));
            }
            String line;
            while ((line = reader.readLine()) != null) {
                if(line.indexOf("--")!=-1 || StringUtils.isBlank(line.trim())) {
                    continue;
                }
                sqls.add(line);
            }
            reader.close();
        } catch (IOException e) {
            logger.info("读取sql 文件: " + filePath + " 文件错误[1]: " + e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.info("读取sql 文件: " + filePath + " 文件错误[2]: " + e.getMessage());
                }
            }
        }
        return sqls.size()!=0?sqls:null;
    }

    @Override
    public void executeSqls(List<String> sqls) {
        if(null == sqls || sqls.size() == 0) {
            logger.info("executeSqls 参数为空: " + sqls);
            return;
        }
        for(String sql : sqls) {
            logger.info("开始执行: " + sql);
            long time = System.currentTimeMillis();
            jdbcTemplate.execute(sql);
            logger.info("执行结束: " + (System.currentTimeMillis()-time)/1000 + "s");
        }
    }

    @Override
    public void updateTranPtnLbsInClean() {
        String sql = "SELECT lbs.rmuid,mtr.eastportrmuid,mtr.westportrmuid FROM tran_ptn_hw_mtr mtr,tran_ptn_lbs lbs"
                + " where 1=1"
                + " and lbs.nermuid=mtr.nermuid and lbs.aendtprmuid=mtr.rmuid"
                + " and lbs.aendportrmuid ='--'";
        List<Map<String, Object>> rs = jdbcTemplate.queryForList(sql);
        if(null != rs && rs.size() > 0) {
            sql = "update tran_ptn_lbs lbs set lbs.aendportrmuid= ? , mpls='1' where lbs.rmuid= ? ";
            for(Map<String, Object> cr : rs) {
                jdbcTemplate.update(sql, cr.get("eastportrmuid"), cr.get("rmuid"));
            }
        }
        sql = "SELECT lbs.rmuid,mtr.eastportrmuid,mtr.westportrmuid FROM tran_ptn_hw_mtr mtr,tran_ptn_lbs lbs"
                + " where 1=1"
                + " and lbs.nermuid=mtr.nermuid and lbs.zendtprmuid=mtr.rmuid"
                + " and lbs.zendportrmuid ='--'";
        rs = jdbcTemplate.queryForList(sql);
        if(null != rs && rs.size() > 0) {
            sql = "update tran_ptn_lbs lbs set lbs.zendportrmuid= ? , mpls='1' where lbs.rmuid = ? ";
            for(Map<String, Object> cr : rs) {
                jdbcTemplate.update(sql, cr.get("eastportrmuid"), cr.get("rmuid"));
            }
        }
    }
}
